<?php
namespace app\admin\controller;
use app\common\controller\Adminbase;
use \think\Loader;
use think\migration\command\migrate\Status;
use think\Request;

/*
** 叫号 控制器
*/

class Queue extends Adminbase {
    private static $_queue = null; 	// 数据表对象

    protected $params;
    public $request;
    // 优先加载
    public function _initialize() {
        $this->request = Request::instance();
        $this->request->filter(['strip_tags','htmlspecialchars']);
        $this->params =  $this->request->param();
        parent::_initialize();
        self::$_queue = Loader::model('Queue');
    }

    /**
     * 叫号数据总览
     * @param page
     * @param page_size
     * @param start_date,end_date,status,created 这几个字段是搜索字段
     * @return \think\response\Json
     * @throws \think\exception\DbException
     * @throws db\exception\DataNotFoundException
     * @throws db\exception\ModelNotFoundException
     */
    public function index() {
        $params = $this->params;
        // 当前页
        $nowPage = 1;
        if(isset($params['page']) && $params['page']){
            $nowPage = intval($params['page']);
            $nowPage = $nowPage > 0 ? $nowPage : 1;
        }

        $pageSize = 10;

        $data = self::$_queue->get_list_for_bk($nowPage,$pageSize,$params);

        $status = 2;
        if (isset($params['status'])) {
            if ($params['status'] == 0 || $params['status'] == 1) {
                $status = $params['status'];
            }
        }

        $data = array(
            'data' 		    => $data['data'],
            'allpages' 		=> $data['allpages'], 	// 总页数
            'nowpage' 		=> $data['nowpage'], 	// 当前页
            'shop_id'       => $params['shop_id'],
            'status'        => $status,
            'startime'      => $params['startime'],
            'endtime'       => $params['endtime'],
        );
        $this -> assign($data);
        return view('index');
    }

    /**
     * 修改叫号的单条配置的状态
     * @param id
     * @param status
     */
    public function edit_status() {
        $params = $this->params;
        $status = $params['status'];
        $id = $params['id'];

        $data = self::$_queue->edit_status($status,$id);
        if ($data) {
            $this -> success('操作成功');
        }
        $this -> error('操作失败');
    }

    /**
     * 获取单条配置数据
     * @param id
     * @param data
     * @return \think\response\View
     */
    public function config_info() {
        $params = $this->params;
        $id = $params['id'];
        $data = self::$_queue->get_info($id);
        $this -> assign(['lists' => $data]);
        return view('edit');
    }

    /**
     * 更新单条数据
     * @param id
     * @return \think\response\Json
     */
    public function update_info() {
        $params = $this->params;
        if (empty($params['id'])) {
            return resultArray(['error'=>'没有id']);
        }
        $id = $params['id'];
        unset($params['id']);
        $params['updated'] = time();
        $res = self::$_queue->update_info($params,$id);
        if ($res) {
            $this -> success('操作成功');
        } else {
            $this -> error('操作失败');
        }
    }

}